package com.demo.controller;

import com.demo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    @RequestMapping(value = "/{formName}")
    public String registerForm(@PathVariable String formName, Model model) {
        User user = new User();
        model.addAttribute("user", user);
        return formName;//动态跳转到页面
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@ModelAttribute @Validated User user, Model model, HttpServletRequest request) {
        //从后台代码获取国际化资源文件中的信息userName
        RequestContext requestContext = new RequestContext(request);
        //通过RequestContext的getMessage方法获取到国际化信息
        String username = requestContext.getMessage("userName");
        System.out.println(username);
        model.addAttribute("user", user);
        return "success";
    }
}
